<template>
  <el-card shadow="hover">
    <!--  条件查询-->
    <el-form :inline="true" :model="studentSearch" class="demo-form-inline">

      <el-form-item>
        <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item >
        <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
      </el-form-item>
      <el-form-item >
        <el-select v-model="studentSearch.cid" placeholder="所在班级">
          <el-option label="所有班级" value="0"/>
          <el-option :label="c.cname" :value="c.cid" v-for="c in classes"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="students"
      style="width: 100%">
      <el-table-column
        prop="sid"
        align="center"
        label="id"/>
      <el-table-column
        prop="sname"
        align="center"
        label="学生姓名"/>

      <el-table-column
        prop="sex"
        align="center"
        label="姓别"/>
      <el-table-column
        prop="age"
        align="center"
        label="年龄"/>
      <el-table-column
        prop="addr"
        align="center"
        label="住址"/>
<!--      <el-table-column-->
<!--        prop="cid"-->
<!--        align="center"-->
<!--        label="所在班级"/>-->
      <el-table-column
        prop="cname"
        align="center"
        label="所在班级"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      align="right">
    </el-pagination>
  </el-card>

</template>


<script>
  import {findStudents,findByPage,search} from '@/api/student'
  import {findClasses} from "@/api/classes";

  export default {
    data() {
      return {
        students: [],
        classes:[],
        studentSearch:{},
        page:1,
        pageSize:5,
        total:100
      }
    },
    created() {
      // 查询所有学生
      // this.findStudents();
      //开始分页查询
      //this.findByPage();
      //查询所有班级
      //this.findClasses();
      //条件查询带分页
      this.search();
    },
    methods: {
      //1.1 查询所有学生
      findStudents(){
        findStudents().then(({data})=>{
          this.students = data.data;
        })
      },
      // 分页查询
      // findByPage(){
      //   findByPage(this.page,this.pageSize).then(({data})=>{
      //     // 更新总记录数
      //     this.total = data.pr.total;
      //     // 更新当前页面的总的记录集合
      //     this.students = data.pr.rows;
      //   })
      // },
      // 查询所有班级
      findClasses(){
        findClasses().then(({data})=>{
          this.classes = data.list;
        })
      },
      // 开始条件查询带分页
      search(){
        search(this.page,this.pageSize,JSON.stringify(this.studentSearch)).then(({data})=>{
          // 更新总记录数
          this.total = data.pr.totalPage;
          // 更新当前页面的总的记录集合
          this.students = data.pr.rows;
        })
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      // 页大小发生改变时调用
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.search();
        //this.search();
      },
      // 当前页大小发生改变时调用
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.page = val;
         this.search();
        //this.search();
      }
    }
  }
</script>

<style scoped>
  .el-pagination{
    margin-top: 50px;
  }
</style>
